package com.spzx.order.receiver;


import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class TestReceiver {
    /**
     * 监听消息
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TEST, durable = "true"),
            key = MqConst.ROUTING_TEST
    ))
    public void testReceive(String msg2, Message msg,Channel channel) throws IOException {
        System.out.println("receive msg: " + new String(msg.getBody()));
        System.out.println("receive msg2: " + msg2);

        channel.basicAck(msg.getMessageProperties().getDeliveryTag(), true);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST, type = "direct", durable = "true"),
            key = MqConst.ROUTING_CONFIRM,
            value = @Queue(value = MqConst.QUEUE_CONFIRM, durable = "true")
    ))
    public void testReceiveConfirm(Message msg, String msg2, Channel channel) throws IOException {

        System.out.println("receive confirm msg: " + new String(msg.getBody()));
        System.out.println("receive confirm msg2: " + msg2);

        channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
    }

}